package greed;

/**
 * @Classname : NonDecreasingArray
 * @Description : 665. 非递减数列
 * https://leetcode.cn/problems/non-decreasing-array/
 * @Author : chentianyu
 * @Date 2023/2/24 00:09
 */


public class NonDecreasingArray {
    public boolean checkPossibility(int[] nums) {
        boolean flag = false;
        for (int i=1; i<nums.length; i++) {
            if (nums[i] < nums[i-1]) {
                if (flag) return false;
                flag = true;
                if (i+1 < nums.length && nums[i+1] < nums[i-1]) nums[i-1] = nums[i];
                else nums[i] = nums[i-1];
                if (i-2 >=0 && nums[i-2] > nums[i-1]) return false;
            }
        }
        return true;
    }
}
